<?php


namespace app\index\controller;


use app\index\validate\BaseValidate;
use think\Controller;
use think\facade\Validate;
use think\validate\ValidateRule;

class TestValidate extends Controller
{
    //验证所有参数, 返回所有不满足的参数
    protected $batchValidate = true;
    //当验证失败时, 返回错误信息
    //protected $failException = true;

    public function notExtends()
    {
        $data = ['trade_name' => '', 'unit_price' => '0', 'unit_score' => '-1'];
        $baseValidate = new BaseValidate();
        $result = $baseValidate->check($data);
        if ($result !== true) {
            dump($baseValidate->getError());
        }
    }

    public function doExtends()
    {
        $data = ['trade_name' => '违禁词1', 'unit_price' => '110', 'unit_score' => '110'];
        $baseValidate = $this->validate($data, BaseValidate::class);
        if (true !== $baseValidate) {
            dump($baseValidate);
            return "验证失败";
        } else {
            return "验证通过";
        }
    }

    public function newValidate()
    {
        $data = ['trade_name' => '违禁词1', 'unit_price' => '1', 'unit_score' => '-10'];
        $baskValidate = new BaseValidate();
        $baskValidate->rule([
            'trade_name|商品名称' => ['require', 'max:19', 'nameFilter:违禁词'],
            'unit_price|商品价格' => 'require|between:1,50000',
            'unit_score|奖励积分' => 'require|between:-10,25000'
        ]);
        if (!$baskValidate->check($data)) {
            dump($baskValidate->getError());
        };
    }

    public function objectValidate()
    {
        $data = ['trade_name' => '违禁品', 'unit_price' => '1', 'unit_score' => '66666'];
        $baskValidate = new BaseValidate();
        $baskValidate->rule([
            'trade_name|商品名称' => function ($val) {
                if ($val === "违禁品") {
                    return '商品名称不能为"违禁品"';
                } elseif ($val === "违禁词") {
                    return '商品名称不能为"违禁词"';
                } else {
                    return true;
                }

            },
            'unit_price|商品价格' => ValidateRule::between("1,29"),
            'unit_score|奖励积分' => ValidateRule::isRequire()->max(5),
        ]);
        if (!$baskValidate->check($data)) {
            dump($baskValidate->getError());
        };
    }

    /**
     * 场景验证1
     */
    public function sceneValidate()
    {
        $data = ['trade_name' => '违禁词', 'unit_price' => '1', 'unit_score' => '66666'];
        $result = $this->validate($data, 'BaseValidate.add');
        if (true !== $result) {
            dump($result);
        }
        $result = $this->validate($data, 'BaseValidate.update');
        if (true !== $result) {
            dump($result);
        }
        $baseValidate = new BaseValidate();
        if (!$baseValidate->scene("delete")->check($data)) {
            dump($baseValidate->getError());
        }

    }

    /**
     * 场景验证2
     */
    public function diySceneValidate()
    {
        $data = ['trade_name' => '违禁词', 'unit_price' => '1', 'unit_score' => '66666'];
        $baseValidate = new BaseValidate();
        if (!$baseValidate->scene("find")->check($data)) {
            dump($baseValidate->getError());
        };
    }

    /**
     * facade门面模式验证
     */
    public function facadeValidate()
    {
        $data = ['trade_name' => '违禁词', 'unit_price' => 1, 'unit_score' => 1];
        $result = Validate::check($data, [
            'trade_name' => 'require',
            'unit_price|商品价格' => ['require', 'number', 'between' => '1,100'],
            'unit_score' => 'require|number|between:1,10'
        ]);
        dump($result);
    }

    public function tokenForm()
    {

        /**
         * 自定义生成token的规则, 经测试, 不能同时生成两个令牌
         */
        $random1 = $this->request->token('__random__', 'sha1');
        $random2 = $this->request->token('__token__', 'rsa');
        return $this->fetch(__FUNCTION__, ['__random__' => $random1, '__token__' => $random2]);
    }

    /**
     * token令牌验证, 经测试, 不能同时生成两个令牌
     */
    public function tokenFormPost()
    {
        $params = request()->param();
        dump($params);
        $result = Validate::check($params, ['username' => 'require', '__token__' => 'token', '__random__' => 'token.__random__']);
        dump($result);
    }

    /**
     * 内置函数例子, 直接再::后面跟上内置函数即可, phpStorm提示功能会失效, 内置函数如何注入允许被调用未知
     */
    public function validatePlus()
    {

        $data = ['trade_name' => '违禁词', 'unit_price' => 1, 'unit_score' => 1, 'phone' => '13073101997'];
        $result = Validate::mobile($data['phone']);
        dump($result);

    }
}